package com.college_career_system.controller;

import com.college_career_system.pojo.dto.UsersLoginDTO;
import com.college_career_system.service.UsersLoginService;
import com.college_career_system.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: Empty
 * @Date: 2022-12-05-23:51
 * @Description:
 */
@Controller
//@CrossOrigin(origins = "http://localhost:8081", allowCredentials = "true", maxAge = 3600)
@RequestMapping("/usersLogin")
@CrossOrigin(origins = Constant.URL, allowCredentials = "true", maxAge = 3600)
public class UsersLoginController {

    private final UsersLoginService usersLoginService;

    public UsersLoginController(UsersLoginService usersLoginService) {
        this.usersLoginService = usersLoginService;
    }


    @PassToken
    @PostMapping("/login")
    @ResponseBody
    public String login(UsersLoginDTO usersLoginDTO){
        System.out.println(usersLoginDTO);
        return usersLoginService.login(usersLoginDTO).toString();
    }


    @UsersLoginToken
    @PostMapping("/getmessage")
    @ResponseBody
    public String getmessage(HttpServletRequest request){
        return ResponseResult.success(TokenUtil.getId(request)).toString();
    }

    /**
     * 得到验证码
     * @param session
     * @param response
     * @return
     * @throws IOException
     */
    @PostMapping("/getCode")
    @ResponseBody
    public String getCode(HttpSession session, HttpServletResponse response) throws IOException {
        return usersLoginService.getCode(session,response).toString();
    }

    /**
     * 使用验证码登录
     * 验证码放在userLogin 里面
     * @param session
     * @param response
     * @param usersLoginDTO
     * @return
     */
    @PostMapping("/loginByCode")
    @ResponseBody
    public String loginByCode(HttpSession session, HttpServletResponse response, UsersLoginDTO usersLoginDTO) {
        return usersLoginService.loginByCode(session,response,usersLoginDTO).toString();
    }
}
